<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Api\Controller;

use Think\Api;

class MemberController extends BaseController {

    private $linkTime = 6;

    public function index() {

        echo $password = md5(base64_encode(md5("wangjie")) . '576hbgh6');


        $memberId = session("member_info.member_id");
        if (!$memberId) {
            $result = ["code" => "20003", "msg" => "请登录"];
            exit(json_encode($result));
        }
        exit(json_encode(session("member_info")));
    }

    /*
     * 登陆
     */

    public function login() {
        if (IS_POST) {
            $result = ["code" => "10000", "msg" => "登陆成功"];
            $telephone = I("telephone", "", "string");
            $passWord = md5(I("password", "", "string"));
            $keepLogin = I("keep_login", "", "string");
            if (preg_mobile($telephone) && $passWord) {
                $param['telephone'] = $telephone;
                $param['password'] = $passWord;
                $url = C("SITE_USER") . '/Member/isMember';
                $memberInfo = Api::request($url, $param, "get", $this->linkTime);
                if ($memberInfo["code"] == "10000") {
                    $memberInfo['data']["image_url"] = C("STATIC_IMG") . $memberInfo['data']["image_url"];
                    unset($memberInfo['data']['password']);
                    if ($keepLogin) {
                        session("member_info", $memberInfo);
                    }
                    $memberInfoJson = json_encode($memberInfo);
                    $memberInfo["data"]["token"] = parent::security($memberInfoJson);
                    exit(json_encode($memberInfo, JSON_UNESCAPED_UNICODE));
                } else {
                    $result = ["code" => "20001", "msg" => "用户名或密码不正确"];
                }
            } else {
                $result = ["code" => "20002", "msg" => "手机号或密码不能为空"];
            }
            exit(json_encode($result));
        }
        $result = ["code" => "10001", "msg" => "传参形式不正确"];
        exit(json_encode($result, JSON_UNESCAPED_UNICODE));
    }

    /*
     * 注册
     */

    public function register() {
        if (IS_POST) {
            $telephone = I("telephone", "", "string");
            $passWord = md5(I("password", "", "string"));
            //验证电话号码是否存在
            $param['telephone'] = $telephone;
            $url = C("SITE_USER") . '/Member/isMember';
            $isMember = Api::request($url, $param, "post", $this->linkTime);
            if ($isMember['code'] == 10000) {
                $result = C("USER_EXISTS");
                exit(json_encode($result, JSON_UNESCAPED_UNICODE));
            }
            $param['password'] = $passWord;
            $url = C("SITE_USER") . '/Member/register';
            $memberInfo = Api::request($url, $param, "post", $this->linkTime);
            if ($memberInfo["code"] == 10000) {
                $result = ['code' => '10000', 'msg' => "注册成功"];
                exit(json_encode($result, JSON_UNESCAPED_UNICODE));
            }
        }
        $result = C("PASS_PARAM_ERROR");
        exit(json_encode($result));
    }

}
